package com.hengpeng.itfinoss.controller.acm;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.AppUser;
import com.hengpeng.itfinbase.persist.ShortcutMenu;
import com.hengpeng.itfinbase.persist.ShortcutMenuNode;
import com.hengpeng.itfinoss.constant.Constants;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.service.ServiceFactory;

@Controller
@RequestMapping("/acm/menu")
public class ShortcutMenuController extends BaseController
{
	@Autowired(required = false)
	protected ServiceFactory serviceFactory;

	@RequestMapping(value = "/list")
	public String list(ModelMap map, HttpServletRequest request)
	{
		Pagination page = new Pagination(request);
		Pagination sharepage = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		AppUser user = (AppUser) request.getSession().getAttribute(Constants.ClientConstants.USER_SESSION_KEY);
		try
		{
			List<ShortcutMenu> list = serviceFactory.getAppUserService().findShortcutMenu(user.getId(), null,
					page.currentPage, page.pageSize);
			Long count = serviceFactory.getAppUserService().findShortcutMenuCount(user.getId(), null);

			List<ShortcutMenu> shareList = serviceFactory.getAppUserService().findShareShortcutMenu(user.getId(),
					sharepage.currentPage, sharepage.pageSize);
			Long shareCount = serviceFactory.getAppUserService().findShareShortcutMenuCount(user.getId());
			sharepage.setEntityCount(Integer.valueOf(shareCount.toString()));
			page.setEntityCount(Integer.valueOf(count.toString()));
			map.put("list", list);
			map.put("page", page);
			map.put("sharelist", shareList);
			map.put("sharepage", sharepage);
			return "acm/menuList";
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/addShareMenu/{id}")
	public String addShareMenu(ModelMap map, @PathVariable Long id, HttpServletRequest request)
	{
		AppUser user = (AppUser) request.getSession().getAttribute(Constants.ClientConstants.USER_SESSION_KEY);
		ShortcutMenu s = serviceFactory.getPersistService().get(ShortcutMenu.class, id);
		ShortcutMenu menu = new ShortcutMenu();
		menu.setIsShare(s.getIsShare());
		menu.setName(s.getName());
		menu.setRemark(s.getRemark());
		menu.setSortLevel(s.getSortLevel());
		menu.setUrl(s.getUrl());
		menu.setUserId(user.getId());
		if (s.getNodeSet() != null && s.getNodeSet().size() > 0)
		{
			Set<ShortcutMenuNode> nodeSet = new HashSet<ShortcutMenuNode>();
			for (ShortcutMenuNode node : s.getNodeSet())
			{
				ShortcutMenuNode n = new ShortcutMenuNode();
				n.setMenu(menu);
				n.setName(node.getName());
				n.setRemark(node.getRemark());
				n.setSortLevel(node.getSortLevel());
				n.setUrl(node.getUrl());
				nodeSet.add(n);
			}
			menu.setNodeSet(nodeSet);
		}
		try
		{
			serviceFactory.getAppUserService().saveShortcutMenu(menu);
			return PageHandlerUtil.returnSuccess(map, new ServiceResult<Boolean>(), "/acm/menu/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/del")
	public String remove(Long[] ids, HttpSession session, ModelMap map)
	{
		if (PageHandlerUtil.validateArrayNullOrEmpty(ids))
		{
			return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
		}
		try
		{
			serviceFactory.getAppUserService().removeShortcutMenu(ids);
			return PageHandlerUtil.returnSuccess(map, new ServiceResult<Boolean>(), "/acm/menu/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/create")
	public String initSave(ModelMap map)
	{
		ShortcutMenu s = new ShortcutMenu();
		s.setSortLevel(10l);
		map.put("sharemenu", s);
		return "acm/menuCreate";
	}

	@RequestMapping(value = "/load/{id}")
	public String load(ModelMap map, @PathVariable Long id)
	{
		ShortcutMenu s = serviceFactory.getPersistService().get(ShortcutMenu.class, id);
		map.put("sharemenu", s);
		return "acm/menuModify";
	}

	@RequestMapping(value = "/detail/{id}")
	public String detail(ModelMap map, @PathVariable Long id)
	{
		ShortcutMenu s = serviceFactory.getPersistService().get(ShortcutMenu.class, id);
		map.put("sharemenu", s);
		return "acm/menuDetail";
	}

	@RequestMapping(value = "/modify")
	public String save(ModelMap map, HttpServletRequest request, Long id, String name, BoolValue isShare,
			Long sortLevel, String url, String remark, String[] linsName, Long[] sorts, String[] urls)
	{
		ShortcutMenu menu = serviceFactory.getPersistService().get(ShortcutMenu.class, id);
		menu.setName(name);
		menu.setSortLevel(sortLevel);
		menu.setUrl(url);
		menu.setRemark(remark);
		if (linsName != null)
		{
			if (PageHandlerUtil.validateArrayNullOrEmpty(sorts, urls) || linsName.length != sorts.length
					|| linsName.length != urls.length)
			{
				return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
			}
			Set<ShortcutMenuNode> nodeSet = new HashSet<ShortcutMenuNode>();
			for (int i = 0; i < linsName.length; i++)
			{
				ShortcutMenuNode node = new ShortcutMenuNode();
				node.setName(linsName[i]);
				node.setSortLevel(sorts[i]);
				node.setUrl(urls[i]);
				node.setMenu(menu);
				nodeSet.add(node);
			}
			menu.setNodeSet(nodeSet);
		}
		AppUser user = (AppUser) request.getSession().getAttribute(Constants.ClientConstants.USER_SESSION_KEY);
		menu.setUserId(user.getId());
		if (user.getLoginName().equals("admin") && isShare == BoolValue.YES)
		{
			menu.setIsShare(BoolValue.YES);
		}
		else
		{
			menu.setIsShare(BoolValue.NO);
		}
		try
		{
			serviceFactory.getAppUserService().modifyShortcutMenu(menu);
			return PageHandlerUtil.returnSuccess(map, new ServiceResult<Boolean>(), "/acm/menu/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/save")
	public String save(ModelMap map, HttpServletRequest request, String name, BoolValue isShare, Long sortLevel,
			String url, String remark, String[] linsName, Long[] sorts, String[] urls)
	{
		if (PageHandlerUtil.validateObjectsNullOrEmpty(name, sortLevel))
		{
			return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
		}
		ShortcutMenu menu = new ShortcutMenu();
		menu.setName(name);
		menu.setSortLevel(sortLevel);
		menu.setUrl(url);
		menu.setRemark(remark);
		if (linsName != null)
		{
			if (PageHandlerUtil.validateArrayNullOrEmpty(sorts, urls) || linsName.length != sorts.length
					|| linsName.length != urls.length)
			{
				return PageHandlerUtil.returnError(map, "数据没有通过验证，请刷新页面后重试！");
			}
			Set<ShortcutMenuNode> nodeSet = new HashSet<ShortcutMenuNode>();
			for (int i = 0; i < linsName.length; i++)
			{
				ShortcutMenuNode node = new ShortcutMenuNode();
				node.setName(linsName[i]);
				node.setSortLevel(sorts[i]);
				node.setUrl(urls[i]);
				node.setMenu(menu);
				nodeSet.add(node);
			}
			menu.setNodeSet(nodeSet);
		}
		AppUser user = (AppUser) request.getSession().getAttribute(Constants.ClientConstants.USER_SESSION_KEY);
		menu.setUserId(user.getId());
		if (user.getLoginName().equals("admin") && isShare == BoolValue.YES)
		{
			menu.setIsShare(BoolValue.YES);
		}
		else
		{
			menu.setIsShare(BoolValue.NO);
		}
		try
		{
			serviceFactory.getAppUserService().saveShortcutMenu(menu);
			return PageHandlerUtil.returnSuccess(map, new ServiceResult<Boolean>(), "/acm/menu/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return PageHandlerUtil.returnError(map, ex);
		}
	}

}
